<template>
  <div>
    <div style="text-align:right;line-height:30px">
      <el-row :gutter="20">
        <el-col :span="16" style="text-align: left">
          <el-button class="tool_button" @click="clickNewButton" type="primary" size="mini" icon="el-icon-plus" plain v-show="permissions.indexOf('fmModuleAdd')>0" >新建模块</el-button>
          <el-button class="tool_button" @click="clickDeleteButton" type="danger" size="mini" icon="el-icon-delete" plain v-show="permissions.indexOf('fmModuleDelete')>0" >删除模块</el-button>
          <el-button class="tool_button" @click="clickRefreshButton" type="info" size="mini" icon="el-icon-refresh" plain>刷新</el-button>
          <el-button class="tool_button" @click="toRichText" type="info" size="mini" icon="el-icon-refresh" plain>富文本</el-button>
          <el-button class="tool_button" @click="toUeditor" type="info" size="mini" icon="el-icon-refresh" plain>富文本</el-button>
          <el-button class="tool_button" @click="toUpload" type="info" size="mini" icon="el-icon-refresh" plain>上传</el-button>
        </el-col>

        <el-col :span="8">
          <div>
            <el-input placeholder="请输入关键字搜索" v-model="inputText" class="input-with-select" size="mini" @keyup.enter.native="clickSearchButton">
              <el-select v-model="select" slot="prepend" placeholder="请选择">
                <el-option label="模块名" value="2"></el-option>
              </el-select>
              <el-button slot="append" icon="el-icon-search" @click="clickSearchButton"></el-button>
            </el-input>
          </div>
        </el-col>

      </el-row>
    </div>
    <el-table
      ref="multipleTable"
      :data="tableData.datas"
      tooltip-effect="dark"
      :stripe="true"
      :border="true"
      empty-text="没有数据"
      size="mini"
      style="width: 100%;margin-top:20px"
      @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        label="序号"
        type="index"
        :index="indexMethod"
      >
      </el-table-column>
      <el-table-column
        prop="moduleName"
        label="模块名"
        width="200">
      </el-table-column>
      <el-table-column
        prop="description"
        label="描述"
        width="220">
      </el-table-column>
      <el-table-column
        prop="order"
        label="顺序"
        width="80">
      </el-table-column>
      <el-table-column
        prop="createUser"
        label="创建人"
        width="130">
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间">
      </el-table-column>
      <el-table-column label="操作" style="margin-right:10px;">
        <template slot-scope="scope">
          <icon name="edit" :scale="2" @click.native="handleUpdate(scope.row)" class="pointcss" v-show="permissions.indexOf('fmModuleUpdate')>0"></icon>
          <!--<i @click="showModuleOrderDialog(scope.row)" v-show="permissions.indexOf('fmModuleOrder')>0"><icon name="order" :scale="2" class="pointcss" ></icon>&nbsp;&nbsp;</i>-->
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top:10px;text-align: center">
      <el-pagination background layout="total,prev, pager, next,jumper" :total="tableData.totals" :current-page="tableData.currentPage" v-on:current-change="pageChange">
      </el-pagination>
    </div>
    <orderModuleComponent :moduleOrderDialog="moduleOrderDialog" :moduleOrderRow="moduleRow" v-on:on-result-change="moduleOrderDialogchange"></orderModuleComponent>
  </div>
</template>

<script type="text/javascript">
  import { getModuleList, getModuleListRemote, deleteModuleRemote } from '@/view/api/moduleManager.js'
  import { yfuErrorToast, yfuSuccessToast } from '@/utils/yfuMessage.js'
  import { yfuPost } from '@/utils/request.js'
  import global_ from '@/utils/global.vue'
  import orderModuleComponent from '@/view/moduleManager/moduleOrder.vue'
  import getPermissionByMenuId from '@/utils/MenuPermissionUtils.js'
  export default {
    components: {
      'orderModuleComponent': orderModuleComponent
    },
    props: ['menuId'],
    data () {
      return {
        permissions: '',
        /* table begin */
        tableData: {
          datas: [],
          currentPage: 1,
          totals: 0
        },
        multipleSelection: [],
        /* table end */

        /* search begin */
        select: '2',
        inputText: '',
        /* search end */

        /* temp begin */
        temp: {
          id: '',
          moduleName: '',
          moduleDescribe: '',
          order: '',
          createUser: '',
          createTime: ''
        },
        moduleRow: {},
        cheeckedDatas: [],
        moduleOrderDialog: false
      }
    },
    created: function () {
      let cThis = this

      getPermissionByMenuId(this.sessiongetJSON('user').id, this.menuId, function (permissionString) {
        cThis.permissions = permissionString
      })
    },
    mounted: function () {
      this.getAllDatas({currentPage: 1, pageSize: 10})
    },
    methods: {
      indexMethod (index) {
        index += 1
        let cp = this.tableData.currentPage - 1
        let indexValue = index + cp * 10
        return indexValue
      },
      toggleSelection (rows) {
        if (rows) {
          rows.forEach(row => {
            this.$refs.multipleTable.toggleRowSelection(row)
          })
        } else {
          this.$refs.multipleTable.clearSelection()
        }
      },
      handleSelectionChange (val) {
        this.multipleSelection = val
      },
      // 获取选中行
      getSelectedRows () {
        return this.multipleSelection
      },
      getAllDatas (param1) {
        let cThis = this
        if (global_.isLocalServer) {
          getModuleList({},
            function (data) {
              console.log(data)
              cThis.tableData = data
            },
            function (error) {
              console.log(error)
            }
          )
        } else {
          yfuPost(process.env.BASE_API, getModuleListRemote.url, param1, function (data) {
            cThis.tableData.totals = data.totals
            cThis.tableData.currentPage = data.currentPage
            cThis.tableData.datas = data.list
          },
          function (error) {
            console.log(error)
          })
        }
      },
      pageChange (toPage) {
        if (toPage < 1) {
          toPage = 1
        }
        let param1 = this.createParam()
        param1.currentPage = toPage
        param1.pageSize = 10
        this.getAllDatas(param1)
      },
      // 构建参数
      createParam (outerSearchParam) {
        let param1 = {}
        if (this.inputText == null || this.inputText === '') {
          return param1
        }
        if (this.select === '1') { // 全部
          param1.all = this.inputText
        } else if (this.select === '2') { // 模块名
          param1.moduleName = this.inputText
        } else if (this.select === '3') { // 创建人
          param1.createUser = this.inputText
        }
        return param1
      },
      // 新建
      clickNewButton () {
        this.$emit('listenToChildNewButtonEvent')
      },
      // 编辑
      handleUpdate (row) {
        this.temp = Object.assign({}, row) // copy obj
        this.$emit('listenToChildEditButtonEvent', Object.assign({}, row))
      },
      // 删除
      clickDeleteButton () {
        let cThis = this
        // 获取选中行的数据
        if (this.multipleSelection.length === 0) {
          yfuErrorToast('请至少选中一行')
        } else {
          this.$confirm('是否确认删除模块？', '删除模块', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            if (!global_.isLocalServer) {
              console.log(cThis.multipleSelection)
              let param1 = {}
              let ids = ''
              for (let selection of cThis.multipleSelection) {
                ids += ',' + selection.id
              }
              param1.ids = ids
              yfuPost(process.env.BASE_API, deleteModuleRemote.url, param1, function () {
                yfuSuccessToast('删除成功')
                cThis.refreshTable()
              })
            } else {
              yfuSuccessToast('删除成功')
            }
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          })
        }
      },
      refreshTable () {
        this.pageChange(1)
      },
      clickRefreshButton () {
        this.refreshTable()
      },

      showModuleOrderDialog (row) {
        this.moduleRow = row
        this.moduleOrderDialog = true
      },
      moduleOrderDialogchange (val) {
        this.moduleOrderDialog = val
      },
      // 点击搜索按钮
      clickSearchButton () {
        this.pageChange(1)
      },
      toRichText () {
        this.$router.push({path: '/yfuRichTextDemo'})
      },
      toUeditor () {
        this.$router.push({path: '/yfuUeditorDemo'})
      },
      toUpload () {
        this.$router.push({path: '/uploadDemo'})
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss">
  .el-select .el-input {
    width: 105px;
  }

</style>
